package com.hgw.easyrocketmq.order.consumer;

import org.apache.rocketmq.client.consumer.DefaultMQPushConsumer;
import org.apache.rocketmq.client.consumer.listener.ConsumeOrderlyContext;
import org.apache.rocketmq.client.consumer.listener.ConsumeOrderlyStatus;
import org.apache.rocketmq.client.consumer.listener.MessageListenerOrderly;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.common.message.MessageExt;

import java.util.List;

/**
 * Description: 顺序消费
 *
 * @author LinHuiBa-YanAn
 * @date 2023/1/14 15:20
 */
public class OrderConsume {
	public static void main(String[] args) throws MQClientException {
		DefaultMQPushConsumer mqPushConsumer = new DefaultMQPushConsumer("group1");
		mqPushConsumer.setNamesrvAddr("124.222.223.222:9876;47.96.232.192:9876");
		mqPushConsumer.subscribe("orderTopic", "*");

		// 注册消息监听器
		mqPushConsumer.registerMessageListener(new MessageListenerOrderly() {
			@Override
			public ConsumeOrderlyStatus consumeMessage(List<MessageExt> msgs, ConsumeOrderlyContext context) {
				for (MessageExt msg : msgs) {
					System.out.println("线程名称：[" + Thread.currentThread().getName() + "]:" + new String(msg.getBody()));
				}
				return ConsumeOrderlyStatus.SUCCESS;
			}
		});

		mqPushConsumer.start();
		System.out.println("消费者启动");
	}
}
